/** @format */

import { computed } from 'vue'
import { CACHE_KEY } from '../contants/configCont'
import { useEntity } from './useEntity'

const { data, content, getData, updateData } = useEntity({
  cacheKey: CACHE_KEY.envInfo,
  path: 'env.json',
})

const isPublish = computed(() => content.value.env === 'publish')

const toggleEnv = async () => {
  if (!data.value) await getData()

  const value = isPublish.value ? 'prod' : 'publish'
  await updateEnv(value)
}

const updateEnv = async (env) => {
  return await updateData({
    env,
  })
}

export const useEnv = () => {
  return {
    isPublish,
    getEnv: getData,
    updateEnv,
    toggleEnv,
  }
}
